﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Groups.Manage
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Groups;
    using SharingCenter.Web.Classes.Messages;
    using System.Xml.Linq;
    using SharingCenter.Web.Classes.SiteEvents;
    public partial class Members : GroupsPageBase
    {
        Sc_Group group;

        protected void Page_Init(object sender, EventArgs e)
        {
            ParseGroupIdFromQueryString();
            IsAdminOrRedirect(member);
            HasRights(groupId, Profile.UserId);
            group = GroupOperations.GetGroupByGroupId(groupId);
            ListView1.ItemCommand += new EventHandler<ListViewCommandEventArgs>(ListView1_ItemCommand);
        }

        void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            Guid userid;
            switch (e.CommandName)
            {
                case "Remove":
                    userid = (Guid)ListView1.DataKeys[((ListViewDataItem)e.Item).DataItemIndex].Value;
                    Classes.Groups.GroupOperations.RemoveUserFromGroup(groupId, userid);
                    SetTopMostMessage("该成员已被请出圈子。", true);
                    RefreshPage();
                    break;
                case "AssignAdminRole":
                    userid = (Guid)ListView1.DataKeys[((ListViewDataItem)e.Item).DataItemIndex].Value;
                    GroupRole roleToAssign = (GroupRole)int.Parse(e.CommandArgument.ToString());
                    GroupOperations.ChangeAdminRole(groupId, userid, roleToAssign);
                    SetTopMostMessage("操作已成功。", true);
                    SendMemberOperationMessage(userid, roleToAssign);
                    AddMemberOperationSiteEvent(userid, roleToAssign);
                    RefreshPage();
                    break;
                default: break;
            }
        }

        void AddMemberOperationSiteEvent(Guid userid, GroupRole role)
        {
            string roleName = string.Empty;
            if (role == GroupRole.GroupAdmin)
            {
                XElement cxt = new XElement("Event",
                new XElement("AdminUserId", Profile.UserId),
                new XElement("GroupId", groupId),
                new XElement("GroupName", group.GroupName),
                new XElement("NewRole", "圈子管理员"));
                SiteEventOperations.AddEvent("Groups", "GroupRoleChanged", "Groups", userid, cxt, true, groupId);
            }
        }

        void SendMemberOperationMessage(Guid userId, GroupRole role)
        {
            // Send message
            string roleName = string.Empty;
            switch (role)
            {
                case GroupRole.GroupAdmin:
                    roleName = "圈子管理员";
                    break;
                case GroupRole.NA:
                    roleName = "普通成员";
                    break;
                default: break;
            }

            XElement cxt = new XElement("Event",
                new XElement("AdminUserId", Profile.UserId),
                new XElement("GroupId", groupId),
                new XElement("GroupName", group.GroupName),
                new XElement("NewRole", roleName));

            MessageOperations.SendSystemMessage("Groups", "GroupRoleChanged", "Groups", Profile.UserId, userId, cxt, true);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            BindGroupMembers();
        }

        void BindGroupMembers()
        {
            ListView1.DataSource = dataContext.GetGroupMembers(groupId);
            ListView1.DataBind();
        }

        public string GetGroupRoleName(int role)
        {
            string ret = string.Empty;
            Classes.enums.GroupRole roleEnum = (SharingCenter.Web.Classes.enums.GroupRole)role;
            switch (roleEnum)
            {
                case SharingCenter.Web.Classes.enums.GroupRole.Owner:
                    ret = "创建人";
                    break;
                case SharingCenter.Web.Classes.enums.GroupRole.EventAdmin:
                    ret = "活动管理员";
                    break;
                case SharingCenter.Web.Classes.enums.GroupRole.GroupAdmin:
                    ret = "管理员";
                    break;
                default: break;
            }
                return ret;
        }
    }
}
